<template>
  <!-- 添加或修改testxxxxx对话框 -->
  <el-dialog :before-close="beforeClose" :title="title" :visible="visible" @update:visible="value => this.$emit('update:visible', value)" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="test" prop="test">
        <el-input v-model="form.test" placeholder="请输入test" />
      </el-form-item>
      <el-form-item label="hello" prop="test2">
        <el-input v-model="form.test2" placeholder="请输入hello" />
      </el-form-item>
      <el-form-item label="ssss" prop="ssss">
        <el-input v-model="form.ssss" placeholder="请输入ssss" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {getTest,addTest, updateTest } from "@/api/tteesstt/test";

    export default {
        name: "TestDialog",
        props:{
          title:{
            type:String,
            default:""
          },
          visible:{
            type:Boolean,
            default:false,
          },
          infoId:{
            type:[String,Number],
            default:"",
            required:true
          }
        },
      data(){
          return {
            form:{},
            rules:{},
          }
      },
      created(){
      },
      methods:{
 //预留的初始化函数,详情拿回来后会调用这里
        initData(data){

        },
        cancel(){
          this.close();
          this.reset();
        },
        /** 提交按钮 */
        submitForm() {
          this.$refs["form"].validate(valid => {
            if (valid) {
              if (this.form.id != null) {
                updateTest(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.close()
                });
              } else {
                addTest(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.close()
                });
              }
            }
          });
        },
// 表单重置
        reset() {
          this.form = {
              id: null,
              test: null,
              test2: null,
              ssss: null
        };
          this.resetForm("form");
        },
        close(){
          this.reset();
          this.$emit('update:visible', false)
          //告诉外面弹框关掉了，用于让外部的列表刷新一下
          this.$emit('close')
        },
        beforeClose(done){
          this.reset();
          done();
        }
      },
      watch:{
        infoId(){
          getTest(this.infoId).then(response => {
            this.form = response.data;
          });
        }
      }
    }
</script>

<style scoped>

</style>
